<?php

/*
 * @author: Nguyen Anh Duc (anhduc.bkhn@gmail.com) 
 */

class MainController extends AdminController
{

    protected function getActionKey()
    {
        return __CLASS__;
    }

    public function init()
    {
        parent::init();
    }

    public function actionIndex()
    {
        $this->layout = 'opd_content';
        $this->render('index');
    }

    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    /*
     * Display the login page
     */

    public function actionLogin()
    {

        if (!Yii::app()->admin->isGuest)
        {
            $this->redirect(array('main/index'));
        }

        $model = new AdminLoginForm();

        //collect user input data 
        if (isset($_POST['AdminLoginForm']))
        {
            $model->attributes = $_POST['AdminLoginForm'];

            if ($model->login())
            {

                $this->redirect(Yii::app()->admin->returnUrl);
            }
            else
            {
                echo CActiveForm::validate($model);
            }
        }

        $this->render('login', array(
            'model' => $model,
        ));
    }

    public function actionLogout()
    {
        $this->layout = 'nonav';
        Yii::app()->admin->logout();
        $this->redirect(array('main/login'));
    }

    public function actionError()
    {
        echo 'loi';
    }

    public function beforeAction($action)
    {

        /**
         *  If not
         */
        if (!$this->isExceptAction() && Yii::app()->admin->isGuest)
        {

            $this->redirect(array('main/login'));
        }

        /**
         * 
         */
        if (!$this->isExceptAction() && Yii::app()->admin->getState('area') !== Yii::app()->params['area']['admincp'])
        {

            $this->redirect(array('main/login'));
        }
        else
        {
            return true;
        }
    }

}